<?php


# Created on: 2011-12-17 11:54:08 131

/**
* Upload an image manually, under an album
*/


#print_r($_POST); print_r($_FILES); die();
/*
Array
(
    [album_id] => 0
    [Submit] => Submit
)
Array
(
    [image] => Array
        (
            [name] => IMG_2941.JPG
            [type] => image/jpeg
            [tmp_name] => D:\xampp\tmp\php52D.tmp
            [error] => 0
            [size] => 1164078
        )

)
*/

$album_id = $variable->post('album_id', 'integer', 0);

# Make sure we want to UPLOAD an image, different to the value of $album_id
if(isset($_POST['album_id']) && isset($_FILES['image']) && $_FILES['image']['error']==0)
{
	$albums = new albums();
	$album_path = $albums->folder_path($album_id, $protection_code='');
	if(is_dir($album_path) && is_writable($album_path))
	{
		$rename_to = tools::unique_code().'.jpg';
		$uploader = new uploader($album_path, true);
		$name = $uploader->store('image', $rename_to);
		$full_path = $album_path.'/'.$name;
		$mime = $_FILES['image']['type'];
		#print_r($_POST); print_r($_FILES); die($name);
		
		$filesize_bytes = filesize($full_path);
		
		$images = new images();
		$images->upload_image($album_id, $name, $filesize_bytes, sha1_file($full_path), $full_path, $mime);
		
		/**
		* @todo Create all assigned image sizes
		* @todo Create all possible image thumbnails
		*/
/*		
		$sizes_sql="
SELECT
	size_id, store_location,
	cropnail_x, cropnail_y,
	image_quality
FROM gallery_albums_sizes
WHERE
	album_id=$album_id
	AND is_active='Y'
	AND is_approved='Y'
;";
*/
/*
Array
(
    [0] => Array
        (
            [size_id] => 1
            [store_location] => D:/thumbs/2/1
            [cropnail_x] => 0
            [cropnail_y] => 0
            [image_quality] => 75
        )
)*/
		#$sizes = $images->arrays($sizes_sql);
		#print_r($sizes);
		#die($sizes_sql);
		
		$messenger = new messenger('success', "Image uploaded successfully to - <strong>{$album_path}</strong>");
	}
	else
	{
		$messenger = new messenger('error', "Cannot write to the upload directory - {$album_path}");
	}
	
	# Preserve some data
	$_GET['id'] = $album_id;
	
	# Or, list out the all uploaded images
	# images-albums.php?id=2&code=80ae11377d5341172230c3939f95fc0e
	# images-details-public.php?id=2&code=7f8e4bd9ad86eae3dcb36ee33a0e7c92
	
	stopper::url("images-upload.php?id={$album_id}");
}
else if(isset($_POST['album_id']))
{
	$messenger = new messenger('error', 'Choose a valid album first under which we can upload images.');
}

$_GET['id'] = $variable->get('id', 'integer', 0);
?>